//
//  RBMainTabBar.swift
//  myWeiBo
//
//  Created by 刘邓诏初 on 16/4/8.
//  Copyright © 2016年 刘邓诏初. All rights reserved.
//

import UIKit

class RBMainTabBar: UITabBar {
    // 定义闭包
    var composeClosure:(() -> ())?

    // 布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 计算每个按钮的宽度
        let width = self.bounds.size.width / CGFloat(RBMainTabBarItemCount)

        // 设置frame
        var index = 0
        for subview in subviews{
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subview.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: bounds.size.height)
                index++
                if index == 2 {
                index++
                }
            }
        }
        
        composeButton.frame = CGRect(x: CGFloat(2) * width, y: 0, width: width, height: bounds.size.height)
    }
    
    // MARK: - 懒加载
    ///加号按钮
    private lazy var composeButton:UIButton = {
        let btn = UIButton()
        // 设置普通状态
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        // 设置高亮状态
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        // 绑定事件
        btn.addTarget(self , action: Selector("composeBtnDidClick"), forControlEvents: .TouchUpInside)
        // 添加到tabBar上
        self.addSubview(btn)
        
        return btn
    }()
    
    // MARK - 按钮点击事件
    @objc private func composeBtnDidClick(){
        // 调用闭包
        composeClosure?()
    }

}
